package com.zhss.redis.set;

import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.Jedis;

import java.util.List;
import java.util.Set;

/**
 * 设置测试演示
 *
 * @Description TODO
 * @Author winston
 * @date 2021/11/01
 */
public class SetTestDemo {
	HostAndPort hostAndPort = new HostAndPort("121.4.79.149", 6379);
	Jedis jedis = new Jedis(hostAndPort);

	public static void main(String[] args) {
		Long userId = 1L;
		Long friendId = 2L;
		Long superStar = 3L;
		SetTestDemo setTestDemo = new SetTestDemo();
		setTestDemo.add(userId, friendId);
		setTestDemo.add(userId, superStar);
		setTestDemo.add(friendId, superStar);

		setTestDemo.getFollowers(superStar).stream().forEach(System.out::println);
	}

	public void add(Long userId, long targetUserId) {
		jedis.sadd("user::" + targetUserId + "::followers", String.valueOf(userId));
	}

	public void remove(Long userId, Long targetUserId) {
		jedis.srem("user::" + targetUserId + "::followers", String.valueOf(userId));
	}

	public Set<String> getFollowers(Long userId) {
		return jedis.smembers("user::" + userId + "::followers");
	}

	/**
	 * 得到交集
	 *
	 * @param myId     我的身份证
	 * @param friendId 朋友id
	 * @return {@link Set}<{@link String}>
	 */
	public Set<String> getIntersections(Long myId, Long friendId) {
		return jedis.sinter("user::" + myId + "followers", "user::" + friendId + "::followers");
	}

	/**
	 * 得到diff
	 *
	 * @param userId      用户id
	 * @param otherUserId 其他的用户id
	 * @return {@link Set}<{@link String}>
	 */
	public Set<String> getDiff(Long userId, long otherUserId) {
		return jedis.sdiff("user::" + userId + "::follower", "user::" + otherUserId + "::follower");
	}

	/**
	 * 随机获得后, 删除元素
	 *
	 * @param eventId 标识符
	 * @param count   数量
	 * @return {@link List}<{@link String}>
	 */
	public List<String> randomGet(long eventId, int count) {
		return jedis.srandmember("lottery_draw_event::" + eventId, count);
	}

	public void sortedSet() {

	}



}
